<?php
require_once 'Zend/Db/Table/Abstract.php';
class StudentsModel extends Zend_Db_Table_Abstract
{
	protected $_name = 'students';
	protected $_id = 'id';
	
	/*
	 * Lay danh sach cac sinh vien
	 */
	public static function getAllStudent(){
		$db = Zend_Registry::get('connectDB');
		$mySql = $db->select()
					->from('students');
		$list = $db->fetchAll($mySql);
		return $list;
	}
	
	/*
	 * Lay danh sach sinh vien co gioi han so luog
	 */
	
	public function selectLimitStudent($count,$limit){
		$db = Zend_Registry::get('connectDB');
		$mysql = $db -> select()
					->from('students')
					->limit($count,$limit)
					->order("id DESC");
		$list = $db->fetchAll($mysql);
		return  $list;
	}
	
	/*
     * Ham lay thong tin sinh vien qua id
     */  
    public function getDbbyID($id){
        $db= Zend_Registry::get('connectDB');
        $mysql= $db->select()
            ->from($this->_name)
            ->where('id=?',$id);
        $list= $db->fetchAll($mysql);
        return $list;
    }
    
    /*
	 * Ham tra ve id cua sv theo ma sv
	 */
	
	public function getIdStudentByShsv($shsv){
		$db = Zend_Registry::get('connectDB');
		$mysql = $db -> select()
					->from('students')
					->where("shsv =?",$shsv);
		$list = $db->fetchAll($mysql);
		if(count($list)) return $list[0]["id"];
		else return 0;
	}
	
	/*
	 * Ham them thong tin sinh vien
	 */
	public function add($sv){
		$model = new StudentsModel();
		$data = array('name'=>$sv['name'],
					'birthday'=>$sv['birthday'],
					'shsv'=>$sv['shsv'],
					'class'=>$sv['class'],
					'username'=>$sv['username'],
					'password'=>$sv['password'],
					'status'=>$sv['status'],
					'course'=>$sv['course']);
		$id = $model->insert($data);
		return $id;
	}

	/*
	 * Ham edit thong tin sinh vien
	 */
	public function edit($sv){
		$model = new StudentsModel();
		$data = array('name'=>$sv['name'],
					'birthday'=>$sv['birthday'],
					'shsv'=>$sv['shsv'],
					'class'=>$sv['class'],
					'username'=>$sv['username'],
					'password'=>$sv['password'],
					'status'=>$sv['status'],
					'course'=>$sv['course']);
		$id = $model->update($data,"id=".$sv["id"]);
		return $id;
	}
	
	/*
	 * Ham chuyen doi tieng viet co dau thanh khong dau
	 */
	
	public static function vi2en($str)
	{
		$arrTViet=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă",
				"ằ","ắ","ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề"
				,"ế","ệ","ể","ễ",
				"ì","í","ị","ỉ","ĩ",
				"ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
				,"ờ","ớ","ợ","ở","ỡ",
				"ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
				"ỳ","ý","ỵ","ỷ","ỹ",
				"đ",
				"À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
				,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
				"È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
				"Ì","Í","Ị","Ỉ","Ĩ",
				"Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
				,"Ờ","Ớ","Ợ","Ở","Ỡ",
				"Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
				"Ỳ","Ý","Ỵ","Ỷ","Ỹ",
				"Đ");
	
		$arrKoDau=array("a","a","a","a","a","a","a","a","a","a","a"
				,"a","a","a","a","a","a",
				"e","e","e","e","e","e","e","e","e","e","e",
				"i","i","i","i","i",
				"o","o","o","o","o","o","o","o","o","o","o","o"
				,"o","o","o","o","o",
				"u","u","u","u","u","u","u","u","u","u","u",
				"y","y","y","y","y",
				"d",
				"a","a","a","a","a","a","a","a","a","a","a"
				,"a","a","a","a","a","a",
				"e","e","e","e","e","e","e","e","e","e","e",
				"i","i","i","i","i",
				"o","o","o","o","o","o","o","o","o","o","o","o"
				,"o","o","o","o","o",
				"u","u","u","u","u","u","u","u","u","u","u",
				"y","y","y","y","y",
				"d");
	
		return str_replace($arrTViet, $arrKoDau, $str);
	}
	
	/*
	 * Ham tra ve 1 chuoi ngau nhien
	 */
	public static function randString($length) {
		$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		$size = strlen( $chars );
		$str = "";
		for( $i = 0; $i < $length; $i++ ) {
			$str .= $chars[ rand( 0, $size - 1 ) ];
		 }
		return $str;
	}
    
        /*
	 * Tìm kiếm .
	 */
 	public function search($shsv_search,$name_search){
 		$db = Zend_Registry::get('connectDB');
 		$mysql = $db ->select('id')
			 		 ->from('students');
 					
		if($shsv_search){
		  $mysql->where("students.shsv LIKE ?",'%'.$shsv_search.'%');
		}
        $mysql->where("students.name LIKE ?",'%'.$name_search.'%');
		
        
	//	echo $mysql->__toString();exit();
		$list = $db->fetchAll($mysql);
		return $list;
 	}
}